<?php


namespace app\service;


use think\paginator\driver\Bootstrap;

class Page
{
    //返回分页对象
    public static function set($items, $listRows, $currentPage, $total, $query, $path = '')
    {
        return Bootstrap::make($items, $listRows, $currentPage, $total, false, [
            'var_page' => 'page',
            'path' => $path ? $path : url(),
            'query' => $query,
            'fragment' => '',
        ]);
    }


    /**
     * bootstrap4风格
     * @param $totalData
     * @param $perPage
     * @param $currentPage
     * @return string
     */
    public static function bootstrap4($totalData, $perPage, $currentPage)
    {
        $totalPages = ceil($totalData / $perPage);
        $paginationHtml = '';

        if ($totalPages > 1) {
            $paginationHtml .= '<ul class="pagination">';

            // Previous page link
            if ($currentPage > 1) {
                $paginationHtml .= '<li class="page-item"><a class="page-link" href="?page=' . ($currentPage - 1) . '">前一页</a></li>';
            }

            // Page links
            for ($i = 1; $i <= $totalPages; $i++) {
                if ($i == $currentPage) {
                    $paginationHtml .= '<li class="page-item active"><a class="page-link" href="?page=' . $i . '">' . $i . '</a></li>';
                } else {
                    $paginationHtml .= '<li class="page-item"><a class="page-link" href="?page=' . $i . '">' . $i . '</a></li>';
                }
            }

            // Next page link
            if ($currentPage < $totalPages) {
                $paginationHtml .= '<li class="page-item"><a class="page-link" href="?page=' . ($currentPage + 1) . '">下一页</a></li>';
            }

            $paginationHtml .= '</ul>';
        }

        return $paginationHtml;
    }
}